package net.ib.mtalk.ui.start;

import net.ib.mntalk.protocol.Common.AccessToken;
import net.ib.mntalk.protocol.Common.AccountToken;
import net.ib.mntalk.protocol.Common.RequestToken;
import net.ib.mntalk.protocol.Common.UserToken;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenRes;
import net.ib.mtalk.R;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.ui.HomeActivity;
import net.ib.mtalk.util.MTalkLogWriter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AccountIdActivity extends Activity {
	
	private EditText editId;
	private EditText editPassword;
	
	private Context mContext;
	
	private AccountIdController mUiController;
	
	private RequestToken mRequestToken;
	private AccountToken mAccountToken;
	private UserToken mUserToken;
	
	private ProgressDialog mProgressDialog;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch(msg.what) {
				case HandlerMsgConst.HANDLER_UNKNOWN_ERROR :
					mProgressDialog.hide();
					Toast.makeText(mContext, "HANDLER_UNKNOWN_ERROR", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_SUCCESS :
					AcquireAccountAuthTokenRes res = (AcquireAccountAuthTokenRes) msg.obj;
					mUiController.acquireAccountToServer(res.getAccountAuthToken());
					Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_AUTH_SUCCESS", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_FAIL :
					mProgressDialog.hide();
					Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_AUTH_FAIL", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_SUCCESS :
					mAccountToken = (AccountToken) msg.obj;
					mUiController.acquireRequestTokenToServer(mAccountToken);
					Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_SUCCESS", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_FAIL :
					mProgressDialog.hide();
					Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_FAIL", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_REQUEST_SUCCESS :
					mRequestToken = (RequestToken) msg.obj;
					mUiController.registerUserToServer(mAccountToken);
					Toast.makeText(mContext, "HANDLER_ACQUIRE_REQUEST_SUCCESS", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_REQUEST_FAIL :
					mProgressDialog.hide();
					Toast.makeText(mContext, "HANDLER_ACQUIRE_REQUEST_FAIL", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_REGISTER_USER_SUCCESS:
					mUserToken = (UserToken) msg.obj;
					mUiController.acquireAccessTokenToServer(mRequestToken, mUserToken);
					Toast.makeText(mContext, "HANDLER_REGISTER_USER_SUCCESS", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_REGISTER_USER_FAIL :
					mProgressDialog.hide();
					new AlertDialog.Builder(mContext).setMessage(getString(R.string.join_id_login_wrong_msg))
					.setNegativeButton(R.string.done, new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}).create().show();
					Toast.makeText(mContext, "HANDLER_REGISTER_USER_FAIL", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_ACCESS_SUCCESS:
					GlobalPreferences.putMessageToPreferences((AccessToken) msg.obj);
					mUiController.authenticateToServer((AccessToken) msg.obj);
					break;
				case HandlerMsgConst.HANDLER_ACQUIRE_ACCESS_FAIL:
					mProgressDialog.hide();
					Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCESS_FAIL", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_AUTHENTICATE_SUCCESS:
					MTalkLogWriter.d("SUCCESS TO LOG-IN");
					mUiController.setSvcAgreement();
					postDelayed(new Runnable(){
						public void run() {
							mUiController.addAllContacts();
							mUiController.getAllView();
						}}, 5000);
					goToHomeActivity();
					mProgressDialog.hide();
					Toast.makeText(mContext, "HANDLER_AUTHENTICATE_SUCCESS", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_AUTHENTICATE_FAIL :
					mProgressDialog.hide();
					Toast.makeText(mContext, "HANDLER_AUTHENTICATE_FAIL", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_SET_SVC_AGREEMENT_SUCCESS:
					Toast.makeText(mContext, "HANDLER_SET_SVC_AGREEMENT_SUCCESS", Toast.LENGTH_SHORT).show();
					break;
				case HandlerMsgConst.HANDLER_SET_SVC_AGREEMENT_FAIL :
					Toast.makeText(mContext, "HANDLER_SET_SVC_AGREEMENT_FAIL", Toast.LENGTH_SHORT).show();
					break;
				default:
					break;
			}
		}
	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_account_id);
		
		mContext = this;
		
		mProgressDialog = new ProgressDialog(mContext);
		mProgressDialog.setMessage(getString(R.string.waiting));
		mProgressDialog.setCancelable(false);
		
		editId = (EditText) findViewById(R.id.editId);
		editPassword = (EditText) findViewById(R.id.editPassword);
		
		mUiController = new AccountIdController(mContext, mHandler);
	}
	
	public void onOkClick(View view) 
	{
		String id = editId.getText().toString();
		String password = editPassword.getText().toString();
		if(id.length() > 3 && password.length() > 5) 
		{
			mUiController.acquireAccountAuthToServer(id, password);
			mProgressDialog.show();
		}
		else if(id.length() == 0 && password.length() == 0)
		{
			new AlertDialog.Builder(mContext).setMessage(getString(R.string.join_id_login_empty_msg))
			.setNegativeButton(R.string.done, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).create().show();
		}
		else
		{
			new AlertDialog.Builder(mContext).setMessage(getString(R.string.join_id_login_wrong_msg))
			.setNegativeButton(R.string.done, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).create().show();
		}
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		if(mProgressDialog != null)
			mProgressDialog.dismiss();
	}
	
	public void goToHomeActivity() {
		Intent intent = new Intent(this, HomeActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		finish();
		startActivity(intent);
	}
}
